﻿using System.Collections.Generic;
using System.Data.SqlClient;

namespace BLL.Services.Responses
{
    public class ResponsesService : IResponsesService
    {
        private readonly CommentsCollection _commentsCollection;

        public ResponsesService(CommentsCollection collection)
        {
            _commentsCollection = collection;
        }

        public Response GetResponseById(int responseId)
        {
            return _commentsCollection.SelectById(responseId);
        }

        public IEnumerable<BookResponse> GetResponseByBook(int bookId)
        {
            return _commentsCollection.GetCommentsByBook(bookId);
        }

        public void Create(Response response)
        {
            try
            {
                var result = _commentsCollection.Create(response);
                response.ResponseId = result.ResponseId;
            }
            catch (SqlException sqlException)
            {
                throw new ServiceException(sqlException.Message);
            }
        }

        public void Save(Response response)
        {
            try
            {
                _commentsCollection.Save(response);
            }
            catch (SqlException sqlException)
            {
                throw new ServiceException(sqlException.Message);
            }
        }

        public void Delete(int responseId)
        {
            try
            {
                _commentsCollection.Delete(responseId);
            }
            catch (SqlException sqlException)
            {
                throw new ServiceException(sqlException.Message);
            }
        }

        public Listing.ResponsesListing GetListingResponse(
            int page, int size, string column, int direction, Listing.ResponseFilter filter)
        {
           return _commentsCollection.GetListingComment(page, size, column, direction, filter);
        }
    }
}
